{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Quantum Measurement\n",
    "\n",
    "[![下载Notebook](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_notebook.png)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/master/mindquantum/zh_cn/mindspore_quantum_measurement.ipynb)&emsp;\n",
    "[![下载样例代码](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_download_code.png)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/master/mindquantum/zh_cn/mindspore_quantum_measurement.py)&emsp;\n",
    "[![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source.png)](https://gitee.com/mindspore/docs/blob/master/docs/mindquantum/docs/source_zh_cn/quantum_measurement.ipynb)\n",
    "\n",
    "## Overview\n",
    "\n",
    "In the design of quantum circuits, we finally need to obtain the results through the measurement operation. When measuring, we need to select a specific ground state for measurement,  the measured results are uncertain, and the quantum state will also randomly collapse to at some ground state we measure.\n",
    "\n",
    "Quantum measurement is described by a set of measurement operators ${M_m}$, these operators act on the state space of the system under test, the index $m$ represents the possible measurement results in the experiment, if before the measurement, the state of the quantum system is $ |\\psi⟩$, then the probability of occurrence of the result $m$ is:\n",
    "\n",
    "$$\n",
    "p(m)=⟨\\psi|M^\\dagger_mM_m|\\psi⟩\n",
    "$$\n",
    "\n",
    "The state collapse of the system after measurement is:\n",
    "\n",
    "$$\n",
    "\\frac{M_m|\\psi⟩}{\\sqrt{⟨\\psi|M^\\dagger_mM_m|\\psi⟩}}\n",
    "$$\n",
    "\n",
    "The measurement operator satisfies the completeness equation:\n",
    "\n",
    "$$\n",
    "\\Sigma_mM^\\dagger_mM_m=I\n",
    "$$\n",
    "\n",
    "The completeness equation expresses the fact that the probabilities sum to 1:\n",
    "\n",
    "$$\n",
    "1=\\Sigma_m p(m)=\\Sigma_m ⟨\\psi|M^\\dagger_mM_m|\\psi⟩\n",
    "$$\n",
    "\n",
    "This equation holds for all $|\\psi⟩$ and is equivalent to the completeness equation, but it is simpler to directly verify the completeness equation, so the completeness equation is used as a constraint.\n",
    "\n",
    "According to the different measurement operators selected, our common measurements are divided into  computational basis measurement, projection measurement, Pauli measurement, etc. MindQuantum provides a wealth of measurement functions and visual display tools, and we use these functions to further learn quantum measurement."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Computational basis measurement\n",
    "\n",
    "We first have a simple understanding of the calculation basis measurement: suppose there is a state of n qubits, we perform n-bit calculation basis measurement on it. After the measurement, if the result is $00 \\cdots0$, it indicates that the quantum of the n-qubit system has collapsed to the $|00 \\cdots0⟩$ state; similarly, if one of the qubits is measured, half of the $2^n$cases it represents are excluded, i.e. the measurement operation projects the quantum state onto one of the two halves of the space, indicating that a subsystem of the quantum state of the n-qubit system has collapsed.\n",
    "\n",
    "### Single-qubit measurement under computational basis\n",
    "\n",
    "Computational base measurement operators: $M_0=|0⟩⟨0|$ and $M_1=|1⟩⟨1|$, notice that each measurement operator is Hermite, that is, $M_0^\\dagger=M_0, M_1^\\dagger=M_1$, and $M^2_0=M_0, M^2_1=M_1$, so the completeness relation is satisfied:\n",
    "\n",
    "$$\n",
    "I=M^\\dagger_0M_0+M^\\dagger_1M_1=M_0+M_1\n",
    "$$\n",
    "\n",
    "Assuming that the measured state $|\\psi⟩=a|0⟩+b|1⟩$, the probability of obtaining the measurement result 0 is:\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "p(0)&=⟨\\psi|M^\\dagger_0M_0|\\psi⟩\\\\\n",
    "&=⟨\\psi|M_0|\\psi⟩\\\\\n",
    "&=⟨\\psi|(|0⟩⟨0|)|\\psi⟩\\\\\n",
    "&=(⟨\\psi|0⟩)(⟨0|\\psi⟩)\\\\\n",
    "&=[(⟨0|a^{\\star}+⟨1|b^{\\star})|0⟩][⟨0|(a|0⟩+b|1⟩)]\\\\\n",
    "&=(a^{\\star}⟨0|0⟩+b^{\\star}⟨1|0⟩)(a⟨0|0⟩+b⟨1|0⟩)\\\\\n",
    "&=a^{\\star}a\\\\\n",
    "&=|a|^2\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "Similarly, the probability of obtaining measurement 1 is $p(1)=|b|^2$. In both cases, the measured states are:\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "\\frac{M_0|\\psi⟩}{|a|}=\\frac{a}{|a|}|0⟩\\\\\n",
    "\\frac{M_1|\\psi⟩}{|b|}=\\frac{b}{|b|}|1⟩\\\\\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "### Measurement of multiple qubits under the computational base - taking two qubits as an example\n",
    "\n",
    "#### Measure all bits in the system\n",
    "\n",
    "Computational basis measurement operator in two-qubit system: $M_{00}=|00⟩⟨00|, M_{01}=|01⟩⟨01|, M_{10}=|10⟩⟨10|$ and $ M_{11}=|11⟩⟨11|$. Notice that each measurement operator is Hermite, that is, it satisfies $M_{ij}^\\dagger=M_{ij},i,j\\in\\{0, 1\\}$, and $M_{ij}^2=M_{ij}$, so the completeness relation is satisfied:\n",
    "\n",
    "$$\n",
    "I=M^\\dagger_{00}M_{00}+M^\\dagger_{01}M_{01}+M^\\dagger_{10}M_{10}+M^\\dagger_{11}M_{11}= M_{00}+M_{01}+M_{10}+M_{11}\n",
    "$$\n",
    "\n",
    "Assuming that the measured state $|\\psi⟩=a|00⟩+b|01⟩+c|10⟩+d|11⟩$, the probability of obtaining the measurement result 00 is:\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "p(00)&=⟨\\psi|M^\\dagger_{00}M_{00}|\\psi⟩\\\\\n",
    "&=⟨\\psi|M_{00}|\\psi⟩\\\\\n",
    "&=⟨\\psi|(|00⟩⟨00|)|\\psi⟩\\\\\n",
    "&=(⟨\\psi|00⟩)(⟨00|\\psi⟩)\\\\\n",
    "&=[(⟨00|a^{\\star}+⟨01|b^{\\star}+⟨10|c^{\\star}+⟨11|d^{\\star})|00⟩][⟨00|(a|00⟩+b|01⟩+c|10⟩+d|11⟩)]\\\\\n",
    "&=(a^{\\star}⟨00|00⟩+b^{\\star}⟨01|00⟩+c^{\\star}⟨10|00⟩+d^{\\star}⟨11|00⟩)(a⟨00|00⟩+b⟨00|01⟩+c⟨00|10⟩+b⟨00|11⟩)\\\\\n",
    "&=a^{\\star}a\\\\\n",
    "&=|a|^2\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "Similarly, the probability of obtaining measurement 01 is $p(01)=|b|^2$, the probability of 10 is $p(10)=|c|^2$, and the probability of 11 is $p(11)= |d|^2$. In the four cases, the states after measurement are:\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "\\frac{M_{00}|\\psi⟩}{|a|}=\\frac{a}{|a|}|00⟩\\\\\n",
    "\\frac{M_{01}|\\psi⟩}{|b|}=\\frac{b}{|b|}|01⟩\\\\\n",
    "\\frac{M_{10}|\\psi⟩}{|c|}=\\frac{c}{|c|}|10⟩\\\\\n",
    "\\frac{M_{11}|\\psi⟩}{|d|}=\\frac{d}{|d|}|11⟩\\\\\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "#### Measuring a single qubit in the system\n",
    "\n",
    "Measuring the first qubit of a two-qubit quantum state, the two-computational basis measurement operator: $M_0=|0⟩⟨0|\\otimes I$ and $M_1=|1⟩⟨1|\\otimes I$. Notice that Each measurement operator is Hermite, that is, it satisfies $M_0^\\dagger=M_0, M_1^\\dagger=M_1$, and $M^2_0=M_0, M^2_1=M_1$, so it satisfies the completeness relation:\n",
    "\n",
    "$$\n",
    "I=M^\\dagger_0M_0+M^\\dagger_1M_1=M_0+M_1\n",
    "$$\n",
    "\n",
    "Assuming that the measured state $|\\psi⟩=a|00⟩+b|01⟩+c|10⟩+d|11⟩$, then measure the first qubit of the two-qubit quantum state, and obtain the measurement result of 0 The probability is:\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "p(0)&=⟨\\psi|M^\\dagger_0M_0|\\psi⟩\\\\\n",
    "&=⟨\\psi|M_0|\\psi⟩\\\\\n",
    "&=⟨\\psi|(|0⟩⟨0|\\otimes I)|\\psi⟩\\\\\n",
    "&=(⟨00|a^{\\star}+⟨01|b^{\\star}+⟨10|c^{\\star}+⟨11|d^{\\star})|(|0⟩⟨0|\\otimes I)|(a|00⟩+b|01⟩+c|10⟩+d|11⟩)\\\\\n",
    "&=(⟨00|a^{\\star}+⟨01|b^{\\star}+⟨10|c^{\\star}+⟨11|d^{\\star})|(a|00⟩+b|01⟩)\\\\\n",
    "&=a^{\\star}a+b^{\\star}b\\\\\n",
    "&=|a|^2+|b|^2\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "Similarly, the probability of getting measurement 1 is $p(1)=|c|^2+|d|^2$. In both cases, the measured states are:\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "\\frac{M_0|\\psi⟩}{\\sqrt{|a|^2+|b|^2}}=\\frac{a}{\\sqrt{|a|^2+|b|^2}}|00⟩+\\frac{b}{\\sqrt{|a|^2+|b|^2}}|01⟩\\\\\n",
    "\\frac{M_1|\\psi⟩}{\\sqrt{|c|^2+|d|^2}}=\\frac{c}{\\sqrt{|c|^2+|d|^2}}|10⟩+\\frac{d}{\\sqrt{|c|^2+|d|^2}}|11⟩\\\\\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "Through the study of computation-based measurements, we can intuitively realize that making a measurement on one of the bits of a multi-qubit state essentially projects the quantum state into one of the two subspaces. To distinguish these two subspaces concisely, we use our knowledge of linear algebra to know that two orthogonal subspaces can be described by matrices with exactly two unique eigenvalues.\n",
    "\n",
    "### MindQuantum implementation for Computational base measurements\n",
    "\n",
    "Next, we use MindQuantum to build a quantum circuit with measurement operations and observe the results. First, import the modules that this tutorial depends on."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np                           # Import the numpy library and abbreviate to np\n",
    "from mindquantum.core import X, H  # Import quantum gate H, X\n",
    "from mindquantum.simulator import Simulator  # Import the Simulator class from mindquantum. simulator\n",
    "from mindquantum.core import Circuit  # Import the Circuit module for building quantum circuits\n",
    "from mindquantum import Measure  # Import measurement gate"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description:\n",
    "\n",
    "(1) Numpy is a powerful Python library, mainly used to perform calculations on multi-dimensional arrays, supports a large number of dimensional array and matrix operations, and also provides a large number of mathematical function libraries for array operations;\n",
    "\n",
    "(2) MindQuantum is a quantum-classical hybrid computing framework that supports the training and reasoning of various quantum neural networks;\n",
    "\n",
    "(3) The quantum gate to be executed in the built quantum circuit needs to be imported from the mindquantum.core module;\n",
    "\n",
    "(4) The quantum simulator required to run the quantum circuit needs to be imported from the mindquantum.simulator module;\n",
    "\n",
    "(5) The quantum circuit class Circuit required to build a quantum circuit needs to be imported from the mindquantum.core module;\n",
    "\n",
    "(6) To measure the quantum circuit, we need to import the measure operation from MindQuantum.\n",
    "\n",
    "We build a quantum circuit that prepares a two-qubit uniform superposition state $|\\psi⟩=\\frac{\\sqrt{2}(|00⟩+|11⟩)}{2}$, and show it separately for the results of using the computational base measurement on all qubits and using the computational base measurement only on qubit 0.\n",
    "\n",
    "#### MindQuantum implements measuring all qubits in the system\n",
    "\n",
    "Before using the code demonstration, let's simply calculate the theoretical value.\n",
    "\n",
    "Use the computational basis to measure $|\\psi⟩=\\frac{\\sqrt{2}(|00⟩+|11⟩)}{2}$ on all qubits:\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "p(00)&=|a|^2=(\\frac{\\sqrt{2}}{{2}})^2=\\frac{1}{2}\\\\\n",
    "p(01)&=|b|^2=0^2=0\\\\\n",
    "p(10)&=|c|^2=0^2=0\\\\\n",
    "p(11)&=|d|^2=(\\frac{\\sqrt{2}}{{2}})^2=\\frac{1}{2}\\\\\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "It can be seen that there are only two possibilities for the measurement result: 00 and 11, and the probability is $\\frac{1}{2}$. The states after measurement are:\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "\\frac{a}{|a|}|00⟩=|00⟩\\\\\n",
    "\\frac{d}{|d|}|11⟩=|11⟩\\\\\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "We start building a quantum circuit that prepares $|\\psi⟩=\\frac{\\sqrt{2}(|00⟩+|11⟩)}{2}$ and measures all bits:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"276.8\" height=\"140\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"276.8\" height=\"140\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<line x1=\"48.8\" x2=\"256.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"256.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"132.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"138.8\" x2=\"166.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<rect x=\"192.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n<circle cx=\"212.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" />\n<path d=\"M 200.0 50.4 A 12.8 12.8 0 0 1 225.60000000000002 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n<path d=\"M 216.90184831748593 33.93539030917347 L 225.21569219381655 29.135390309173467 L 225.21569219381655 38.73539030917347 L 221.8901546432843 36.815390309173466 L 214.04707658144957 50.4 L 212.38430780618347 49.44 L 220.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" />\n<rect x=\"192.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n<circle cx=\"212.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" />\n<path d=\"M 200.0 110.4 A 12.8 12.8 0 0 1 225.60000000000002 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n<path d=\"M 216.90184831748593 93.93539030917347 L 225.21569219381655 89.13539030917347 L 225.21569219381655 98.73539030917347 L 221.8901546432843 96.81539030917347 L 214.04707658144957 110.4 L 212.38430780618347 109.44 L 220.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f162cae3e20>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "circ_all = Circuit()  # Initialize the quantum circuit\n",
    "circ_all += H.on(0)  # The H gate acts on qubit 0\n",
    "circ_all += X.on(1,\n",
    "                 0)  # The X gate acts on qubit 1 and is controlled by qubit 0\n",
    "circ_all += Measure('q0').on(\n",
    "    0)  # Apply a measurement on qubit 0 and name this measurement 'q0'\n",
    "circ_all += Measure('q1').on(\n",
    "    1)  # Apply a measurement to qubit 1 and name this measurement 'q1'\n",
    "circ_all.svg()  # Draw a quantum circuit picture in SVG format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"408.4\" height=\"117.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"408.4\" height=\"117.0\" fill=\"#ffffff\" />\n<text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nShots:\n 1\n </text>\n<text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nKeys: q0 q1\n </text>\n<line x1=\"38.4\" x2=\"398.4\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"38.4\" x2=\"38.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"40.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.0\n </text>\n<line x1=\"38.4\" x2=\"38.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"98.4\" x2=\"98.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"100.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.2\n </text>\n<line x1=\"98.4\" x2=\"98.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"158.4\" x2=\"158.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"160.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.4\n </text>\n<line x1=\"158.4\" x2=\"158.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"218.4\" x2=\"218.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"220.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.6\n </text>\n<line x1=\"218.4\" x2=\"218.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"278.4\" x2=\"278.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"280.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.8\n </text>\n<line x1=\"278.4\" x2=\"278.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"338.4\" x2=\"338.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"340.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n1.0\n </text>\n<line x1=\"338.4\" x2=\"338.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<text x=\"29.4\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n11\n </text>\n<line x1=\"31.4\" x2=\"38.4\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"38.4\" y=\"73.0\" width=\"300.0\" height=\"24\" id=\"bar_0_1649988508798485442\" fill=\"#5e7ce0\" />\n<text x=\"348.4\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1649988508798514190\" fill-opacity=\"0\" >\n1\n </text>\n<animate xlink:href=\"#bar_0_1649988508798485442\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_0_1649988508798485442\" attributeName=\"fill\" from=\"#5e7ce0\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#5e7ce0; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_0_1649988508798514190\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<text x=\"206.7\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >\nprobability\n </text>\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7f162ca6ea00>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim = Simulator('projectq', 2)  #Declare a 2-bit ProjectQ emulator\n",
    "sim.apply_circuit(circ_all).svg()  # Run the quantum circuit on the simulator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It can be seen that the measurement result we get is '00', and the quantum state after measurement collapses to:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1¦11⟩\n"
     ]
    }
   ],
   "source": [
    "print(sim.get_qs(True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The quantum state collapses to $1|00⟩$, which is consistent with the theoretical value.\n",
    "\n",
    "If we measure a few more times, we can find that the measurement result will also be '11':"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"408.4\" height=\"117.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"408.4\" height=\"117.0\" fill=\"#ffffff\" />\n<text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nShots:\n 1\n </text>\n<text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nKeys: q0 q1\n </text>\n<line x1=\"38.4\" x2=\"398.4\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"38.4\" x2=\"38.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"40.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.0\n </text>\n<line x1=\"38.4\" x2=\"38.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"98.4\" x2=\"98.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"100.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.2\n </text>\n<line x1=\"98.4\" x2=\"98.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"158.4\" x2=\"158.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"160.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.4\n </text>\n<line x1=\"158.4\" x2=\"158.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"218.4\" x2=\"218.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"220.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.6\n </text>\n<line x1=\"218.4\" x2=\"218.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"278.4\" x2=\"278.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"280.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.8\n </text>\n<line x1=\"278.4\" x2=\"278.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"338.4\" x2=\"338.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"340.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n1.0\n </text>\n<line x1=\"338.4\" x2=\"338.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<text x=\"29.4\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n00\n </text>\n<line x1=\"31.4\" x2=\"38.4\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"38.4\" y=\"73.0\" width=\"300.0\" height=\"24\" id=\"bar_0_1649988508969500642\" fill=\"#5e7ce0\" />\n<text x=\"348.4\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1649988508969531308\" fill-opacity=\"0\" >\n1\n </text>\n<animate xlink:href=\"#bar_0_1649988508969500642\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_0_1649988508969500642\" attributeName=\"fill\" from=\"#5e7ce0\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#5e7ce0; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_0_1649988508969531308\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<text x=\"206.7\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >\nprobability\n </text>\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7f15ec8254f0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim.reset()  #Reset simulator\n",
    "sim.apply_circuit(circ_all).svg()  # Run the quantum circuit on the simulator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Printing out the quantum state at this time, we can see that it collapses into the corresponding $|11⟩$:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1¦00⟩\n"
     ]
    }
   ],
   "source": [
    "print(sim.get_qs(True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We observe that the results are sometimes '00' and sometimes '11', which is in line with the theoretical expectation, but there is no way to observe whether the probabilities of 00 and 11 are the same. We hope to make multiple measurements and count the frequency of different results, so as to see whether the results meet the expected probability distribution. For this purpose, we use the quantum line Sampling function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"408.4\" height=\"147.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"408.4\" height=\"147.0\" fill=\"#ffffff\" />\n<text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nShots:\n 1000\n </text>\n<text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nKeys: q0 q1\n </text>\n<line x1=\"38.4\" x2=\"398.4\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"38.4\" x2=\"38.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"40.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.0\n </text>\n<line x1=\"38.4\" x2=\"38.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"98.4\" x2=\"98.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"100.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.101\n </text>\n<line x1=\"98.4\" x2=\"98.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"158.4\" x2=\"158.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"160.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.202\n </text>\n<line x1=\"158.4\" x2=\"158.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"218.4\" x2=\"218.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"220.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.303\n </text>\n<line x1=\"218.4\" x2=\"218.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"278.4\" x2=\"278.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"280.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.404\n </text>\n<line x1=\"278.4\" x2=\"278.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"338.4\" x2=\"338.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"340.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.505\n </text>\n<line x1=\"338.4\" x2=\"338.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<text x=\"29.4\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n00\n </text>\n<line x1=\"31.4\" x2=\"38.4\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"38.4\" y=\"73.0\" width=\"294.05940594059405\" height=\"24\" id=\"bar_0_1649988509242993340\" fill=\"#5e7ce0\" />\n<text x=\"342.459405940594\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1649988509243015428\" fill-opacity=\"0\" >\n495\n </text>\n<text x=\"29.4\" y=\"115.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n11\n </text>\n<line x1=\"31.4\" x2=\"38.4\" y1=\"115.0\" y2=\"115.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"38.4\" y=\"103.0\" width=\"300.0\" height=\"24\" id=\"bar_1_1649988509243035485\" fill=\"#16acff\" />\n<text x=\"348.4\" y=\"115.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_1_1649988509243054811\" fill-opacity=\"0\" >\n505\n </text>\n<animate xlink:href=\"#bar_0_1649988509242993340\" attributeName=\"width\" from=\"0\" to=\"294.05940594059405\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 294.05940594059405\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_1_1649988509243035485\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_1_1649988509243035485\" attributeName=\"fill\" from=\"#16acff\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#16acff; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_0_1649988509243015428\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_1_1649988509243054811\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<text x=\"206.7\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >\nprobability\n </text>\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7f15caaffdc0>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim.reset()\n",
    "result = sim.sampling(circ_all,\n",
    "                      shots=1000)  # Sample the circuit defined above 1000 times\n",
    "result.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that in the sampling 1000, '00' appears 499 times, and '11' appears 501 times. The sampling result conforms to the probability distribution, and the slight error is caused by the noise of the simulator. Students who read it carefully can find that in the [Quantum Simulator Tutorial] (https://gitee.com/buyulin/mindquantum/blob/master/tutorials/quantum_simulator.ipynb), we have shown the sampling results of this circuit, but the reason for the distribution of the results is not explained. After learning the computation based measurement in this tutorial, we believe that you have a deeper understanding of the distribution of the results.\n",
    "\n",
    "#### MindQuantum implements measurement of a single bit in the system\n",
    "\n",
    "Again, let's simply calculate the theoretical value before using the code to demonstrate it.\n",
    "\n",
    "Using the computational basis to measure $|\\psi⟩=\\frac{\\sqrt{2}(|00⟩+|11⟩)}{2}$ on qubit 0:\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "p(0)=|a|^2+|b|^2=(\\frac{\\sqrt{2}}{{2}})^2=\\frac{1}{2}\\\\\n",
    "p(1)=|c|^2+|d|^2=(\\frac{\\sqrt{2}}{{2}})^2=\\frac{1}{2}\\\\\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "It can be seen that there are two possibilities for the measurement result: 0 and 1, and the probability is $\\frac{1}{2}$. The states after measurement are:\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "\\frac{a}{\\sqrt{|a|^2+|b|^2}}|00⟩+\\frac{b}{\\sqrt{|a|^2+|b|^2}}|01⟩=|00⟩\\\\\n",
    "\\frac{c}{\\sqrt{|c|^2+|d|^2}}|10⟩+\\frac{d}{\\sqrt{|c|^2+|d|^2}}|11⟩=|11⟩\\\\\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "We start to build a quantum circuit that prepares $|\\psi⟩=\\frac{\\sqrt{2}(|00⟩+|11⟩)}{2}$ and measures on qubit 0:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"276.8\" height=\"140\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"276.8\" height=\"140\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<line x1=\"48.8\" x2=\"256.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"256.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"132.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"138.8\" x2=\"166.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<rect x=\"192.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n<circle cx=\"212.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" />\n<path d=\"M 200.0 50.4 A 12.8 12.8 0 0 1 225.60000000000002 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n<path d=\"M 216.90184831748593 33.93539030917347 L 225.21569219381655 29.135390309173467 L 225.21569219381655 38.73539030917347 L 221.8901546432843 36.815390309173466 L 214.04707658144957 50.4 L 212.38430780618347 49.44 L 220.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f15c328d310>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "circ_partial = Circuit()  # Initialize the quantum circuit\n",
    "circ_partial += H.on(0)  # The H gate acts on qubit 0\n",
    "circ_partial += X.on(\n",
    "    1, 0)  # The X gate acts on qubit 1 and is controlled by qubit 0\n",
    "circ_partial += Measure('q0').on(\n",
    "    0)  # Apply a measurement on qubit 0 and name this measurement 'q0'\n",
    "circ_partial.svg()  # Draw a quantum circuit picture in SVG format\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"401.2\" height=\"117.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"401.2\" height=\"117.0\" fill=\"#ffffff\" />\n<text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nShots:\n 1\n </text>\n<text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nKeys: q0\n </text>\n<line x1=\"31.2\" x2=\"391.2\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"31.2\" x2=\"31.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"33.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.0\n </text>\n<line x1=\"31.2\" x2=\"31.2\" y1=\"62.0\" y2=\"107.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"91.2\" x2=\"91.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"93.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.2\n </text>\n<line x1=\"91.2\" x2=\"91.2\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"151.2\" x2=\"151.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"153.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.4\n </text>\n<line x1=\"151.2\" x2=\"151.2\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"211.2\" x2=\"211.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"213.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.6\n </text>\n<line x1=\"211.2\" x2=\"211.2\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"271.2\" x2=\"271.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"273.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.8\n </text>\n<line x1=\"271.2\" x2=\"271.2\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"331.2\" x2=\"331.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"333.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n1.0\n </text>\n<line x1=\"331.2\" x2=\"331.2\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<text x=\"22.2\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n0\n </text>\n<line x1=\"24.2\" x2=\"31.2\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"31.2\" y=\"73.0\" width=\"300.0\" height=\"24\" id=\"bar_0_1649988509362739310\" fill=\"#5e7ce0\" />\n<text x=\"341.2\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1649988509362766672\" fill-opacity=\"0\" >\n1\n </text>\n<animate xlink:href=\"#bar_0_1649988509362739310\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_0_1649988509362739310\" attributeName=\"fill\" from=\"#5e7ce0\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#5e7ce0; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_0_1649988509362766672\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<text x=\"203.1\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >\nprobability\n </text>\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7f162ca8fe80>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim.reset()  #Reset simulator\n",
    "sim.apply_circuit(circ_partial).svg()  # Run the quantum circuit on the simulator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It can be seen that the measurement result we get is '0', and the quantum state after measurement collapses to:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1¦00⟩\n"
     ]
    }
   ],
   "source": [
    "print(sim.get_qs(True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The quantum state collapses to $1|00⟩$, which is consistent with the theoretical value.\n",
    "\n",
    "Similarly, if we measure a few more times, we can find that the measurement result will also be '1', which will not be demonstrated here. We directly sample 1000 observations of this quantum circuit:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"401.2\" height=\"147.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"401.2\" height=\"147.0\" fill=\"#ffffff\" />\n<text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nShots:\n 1000\n </text>\n<text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nKeys: q0\n </text>\n<line x1=\"31.2\" x2=\"391.2\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"31.2\" x2=\"31.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"33.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.0\n </text>\n<line x1=\"31.2\" x2=\"31.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"91.2\" x2=\"91.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"93.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.102\n </text>\n<line x1=\"91.2\" x2=\"91.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"151.2\" x2=\"151.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"153.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.205\n </text>\n<line x1=\"151.2\" x2=\"151.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"211.2\" x2=\"211.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"213.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.307\n </text>\n<line x1=\"211.2\" x2=\"211.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"271.2\" x2=\"271.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"273.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.41\n </text>\n<line x1=\"271.2\" x2=\"271.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"331.2\" x2=\"331.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"333.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.512\n </text>\n<line x1=\"331.2\" x2=\"331.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<text x=\"22.2\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n0\n </text>\n<line x1=\"24.2\" x2=\"31.2\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"31.2\" y=\"73.0\" width=\"285.9375\" height=\"24\" id=\"bar_0_1649988509781221380\" fill=\"#5e7ce0\" />\n<text x=\"327.1375\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1649988509781242592\" fill-opacity=\"0\" >\n488\n </text>\n<text x=\"22.2\" y=\"115.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n1\n </text>\n<line x1=\"24.2\" x2=\"31.2\" y1=\"115.0\" y2=\"115.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"31.2\" y=\"103.0\" width=\"300.0\" height=\"24\" id=\"bar_1_1649988509781262122\" fill=\"#16acff\" />\n<text x=\"341.2\" y=\"115.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_1_1649988509781279156\" fill-opacity=\"0\" >\n512\n </text>\n<animate xlink:href=\"#bar_0_1649988509781221380\" attributeName=\"width\" from=\"0\" to=\"285.9375\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 285.9375\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_1_1649988509781262122\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_1_1649988509781262122\" attributeName=\"fill\" from=\"#16acff\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#16acff; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_0_1649988509781242592\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_1_1649988509781279156\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<text x=\"203.1\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >\nprobability\n </text>\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7f15cab14fa0>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim.reset()\n",
    "result = sim.sampling(circ_partial, shots=1000)  #Sample the circuit defined above 1000 times\n",
    "result.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that in sampling 1000, '0' appears 499 times and '1' appears 501 times. The sampling result conforms to the probability distribution, and the slight error is caused by the noise of the simulator.\n",
    "\n",
    "We have completed the study of quantum computational based measurement, and then we enter the study of another measurement operation: projection measurement.\n",
    "\n",
    "## Projection measurement\n",
    "\n",
    "Projection measurement is described by an observable Hermite operator $M$ on the state space of the observed system ($M=M^{\\dagger}$), which has a spectral decomposition:\n",
    "\n",
    "$$\n",
    "M=\\Sigma_{m}mP_m\n",
    "$$\n",
    "\n",
    "Here $P_m$ is the projection on the feature space corresponding to the eigenvalue $m$ of $m$, and the possible results of the measurement correspond to the eigenvalue $m$ of the measurement operator. When measuring the state $|\\psi⟩$, the probability of getting the result $m$ is\n",
    "\n",
    "$$\n",
    "p(m)=⟨\\psi|P_m|\\psi⟩\n",
    "$$\n",
    "\n",
    "The state of the quantum system immediately after the measurement is:\n",
    "\n",
    "$$\n",
    "\\frac{P_m|\\psi⟩}{\\sqrt{p(m)}}\n",
    "$$\n",
    "\n",
    "The intuitive explanation is that we use the $M$ projection measurement for the state $|\\psi⟩$, which is to project $|\\psi⟩$ onto the feature space of $M$, and project the probability of $p_m$ to the space $V_{ m}$, at this time the measurement result is the eigenvalue $m$ corresponding to the space.\n",
    "\n",
    "An important feature of projection measurement is that it is easy to calculate the expected value of projection measurement $E(M)$.\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "    E(M) &=\\Sigma_i \\lambda_i p_i\\\\\n",
    "         &=\\Sigma_i \\lambda_i⟨\\psi|P_i|\\psi⟩\\\\\n",
    "         &=⟨\\psi|(\\Sigma_i\\lambda_i P_i)|\\psi⟩\\\\\n",
    "         &=⟨\\psi|M|\\psi⟩\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "Projection measurement can be regarded as a special case of general measurement. When the measurement operator not only satisfies the completeness relation $\\Sigma_mM_m^\\dagger M_m=I$, but also satisfies the condition that $M_m$ is an orthogonal projection operator, that is, $M_m$ is Hermite, and\n",
    "\n",
    "$$\n",
    "M_mM_{m'}=\\delta_{mm'}M_m\n",
    "$$\n",
    "\n",
    "With these additional constraints, general measurement degenerates into projection measurement.\n",
    "\n",
    "## Pauli Measurement\n",
    "\n",
    "Finally, we learn the Pauli measurement. Pauli measurement is a projection measurement that selects the observable $M$ as the Pauli operator. Taking the Pauli-Z measurement as an example, we consider the Z operator:\n",
    "\n",
    "$$\n",
    "Z=\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    1&0\\\\\n",
    "    0&-1\n",
    "    \\end{array}\n",
    "\\right)\n",
    "$$\n",
    "\n",
    "It can be seen that Z satisfies $Z=Z^\\dagger$, that is, Z is Hermite. Z has two eigenvalues +1, -1, and the corresponding eigenvectors are: |0⟩ and |1⟩. Therefore, the spectral decomposition form of Z is:\n",
    "\n",
    "$$\n",
    "Z=\\left(\n",
    "    \\begin{array}{l}\n",
    "    1&0\\\\\n",
    "    0&-1\n",
    "    \\end{array}\n",
    "\\right)=1\\times|0⟩⟨0|+(-1)\\times|1⟩⟨1|\n",
    "$$\n",
    "\n",
    "Using Z to do projection measurement, if the measurement result is +1, we can conclude that the state of the qubit is projected into the +1 eigensubspace $V_{+1}$ of the Z operator, indicating that the measured state is projected as and |0⟩, similarly, if the measurement result is -1, it can be concluded that the qubit is projected into the -1 eigensubspace $V_{-1}$, indicating that the measured state is projected to be |1⟩, This is known as the Pauli-Z measurement.\n",
    "\n",
    "MindQuantum provides us with the ability to calculate the expected value of a projected measurement based on a given observable H:\n",
    "\n",
    "`get_expectation(hamiltonian)` can calculate the expected value of the current quantum state of the simulator with respect to an observed quantity: $E=⟨\\psi|H|\\psi⟩$. **This operation does not change the quantum state**.\n",
    "\n",
    "For example, we wish to apply a Pauli-Z measurement to the q1 qubit on a system in the state $\\frac{\\sqrt{2}}{2}|00⟩+\\frac{\\sqrt{2}}{2}|11⟩$, first we set the simulator:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.70710678+0.j 0.        +0.j 0.        +0.j 0.70710678+0.j]\n"
     ]
    }
   ],
   "source": [
    "sim = Simulator('projectq', 2)                        # Declare a 2-bit ProjectQ simulator\n",
    "sim.set_qs(np.array([2**0.5 / 2, 0, 0, 2**0.5 / 2]))  # Setting simulator State\n",
    "print(sim.get_qs())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then we construct the Hamiltonian hams corresponding to Pauli-Z measurement on q1:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mindquantum import Hamiltonian  # Import Hamiltonian definition module\n",
    "from mindquantum.core.operators import QubitOperator  # Import sparse operator definition module\n",
    "\n",
    "hams = Hamiltonian(QubitOperator(\n",
    "    'Z1'))  # Construct the Hamiltonian for Pauli-Z measurement on q1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to deeply understand and learn the Pauli-Z measurement operation, we first manually calculate the expected value of the current quantum state of the simulator to perform the Pauli-Z measurement on q1, and calculate the probability that the measurement result is +1, -1:\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "E&=⟨\\psi|H|\\psi⟩\\\\&=\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "(Z \\otimes I) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\\\&=\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    1&0\\\\\n",
    "    0&-1\\\\\n",
    "    \\end{array}\n",
    "\\right) \\otimes\n",
    "\\left(\n",
    "\\begin{array}{l}\n",
    "    1&0\\\\\n",
    "    0&1\\\\\n",
    "\\end{array}\n",
    "\\right)\n",
    "\\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\\\&=\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    1&0&0&0\\\\\n",
    "    0&1&0&0\\\\\n",
    "    0&0&-1&0\\\\\n",
    "    0&0&0&-1\n",
    "    \\end{array}\n",
    "\\right)\n",
    "\\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\\\&=\n",
    "0\\\\\n",
    "&=1\\times p(1)+(-1)\\times p(-1)\\\\\n",
    "&=1\\times p(1)+(-1)\\times (1-p(1))\\\\\n",
    "&=p(1)-1+p(-1)\\\\\n",
    "\\Longrightarrow&p(1)=p(-1)=0.5\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "This means that the theoretical expectation value of the measurement is 0, and the probability of measuring +1 and -1 is 50%. We use the `get_expectation()` provided by MindQuantum to verify the result:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0j"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim.get_expectation(\n",
    "    hams\n",
    ")  # Calculate the expected value of the current quantum state of the simulator with respect to Hams"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see, the result of the manual calculation and the calculation using `get_expectation(hamiltonian)` is the same, as expected.\n",
    "\n",
    "We can also act on both the q0 and q1 qubits on the system in the $\\frac{\\sqrt{2}}{2}|00⟩+\\frac{\\sqrt{2}}{2}|11⟩$ state Pauli-Z measurement. Similarly, the Hamiltonian hams2 corresponding to Pauli-Z measurement on q0, q1 is constructed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "hams2 = Hamiltonian(\n",
    "    QubitOperator('Z0') + QubitOperator('Z1')\n",
    ")  # Construct the Hamiltonian for Pauli-Z measurements on q0,q1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also manually calculate the expected value of the Pauli-Z measurement of the current quantum state of the simulator on q0, q1:\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "E&=⟨\\psi|H|\\psi⟩\\\\&=\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "(Z \\otimes I) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right)\n",
    "+\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "(I \\otimes Z) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\\\&=\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    1&0\\\\\n",
    "    0&-1\\\\\n",
    "    \\end{array}\n",
    "\\right) \\otimes\n",
    "\\left(\n",
    "\\begin{array}{l}\n",
    "    1&0\\\\\n",
    "    0&1\\\\\n",
    "\\end{array}\n",
    "\\right)\n",
    "\\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right)\n",
    "+\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    1&0\\\\\n",
    "    0&1\\\\\n",
    "    \\end{array}\n",
    "\\right) \\otimes\n",
    "\\left(\n",
    "\\begin{array}{l}\n",
    "    1&0\\\\\n",
    "    0&-1\\\\\n",
    "\\end{array}\n",
    "\\right)\n",
    "\\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\\\&=\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    1&0&0&0\\\\\n",
    "    0&1&0&0\\\\\n",
    "    0&0&-1&0\\\\\n",
    "    0&0&0&-1\n",
    "    \\end{array}\n",
    "\\right)\n",
    "\\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right)\n",
    "+\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    1&0&0&0\\\\\n",
    "    0&-1&0&0\\\\\n",
    "    0&0&1&0\\\\\n",
    "    0&0&0&-1\n",
    "    \\end{array}\n",
    "\\right)\n",
    "\\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\\\&=\n",
    "0+0 \\\\\n",
    "&=0\n",
    "\\end{align*}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0j"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim.set_qs(np.array([2**0.5 / 2, 0, 0, 2**0.5 / 2]))  # Set simulator state\n",
    "sim.get_expectation(\n",
    "    hams2\n",
    ")  # Calculate the expected value of the simulator current quantum state about hams2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This operation does not change the quantum state, we look at the current quantum state:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.70710678+0.j])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim.get_qs()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It can be found that the quantum state is still the original set $\\frac{\\sqrt{2}}{2}|00⟩+\\frac{\\sqrt{2}}{2}|11⟩$\n",
    "\n",
    "We learned to recognize an important operation in quantum computing - measurement, used MindQuantum to measure quantum circuits to verify our theoretical results, and used different visualization tools to display the measurement results.\n",
    "\n",
    "To learn higher-level operations on quantum circuits in MindQuantum, build and train quantum-classical hybrid neural networks, see the documentation for `get_expectation_with_grad()` and `apply_hamitonian()`.\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
